/*
 * @lc app=leetcode id=121 lang=cpp
 *
 * [121] Best Time to Buy and Sell Stock
 */
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int len = prices.size();
        if (len < 2) {
            return 0;
        }
        int profit = 0;
        int maxProfit = 0;
        int minPrice = prices[0];

        for (int i=1; i<len; i++) {
            if (minPrice > prices[i]) {
                minPrice = prices[i];
            }
            profit = prices[i] - minPrice;
            if (profit > maxProfit) {
                maxProfit = profit;
            }
        }

        return maxProfit;
    }
};

